/*
 * MyGWT User Extensions
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.mygwt.ux.client.widgets;

import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Label;
import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.SelectionListener;
import net.mygwt.ui.client.widget.Button;
import net.mygwt.ui.client.widget.Dialog;
import net.mygwt.ui.client.widget.WidgetContainer;
import net.mygwt.ui.client.widget.layout.BorderLayout;
import net.mygwt.ui.client.widget.layout.BorderLayoutData;
import net.mygwt.ui.client.widget.layout.FillLayout;

/**
 * 
 * @author mgarin Date: 03.12.2007 Time: 16:42:52
 * 
 */
public class InputDialog extends Dialog {
  private String text;

  private Label label;
  private TextBox textBox;

  private Button okButton;
  private Button cancelButton;

  public InputDialog() {
    super(Style.MODAL);
    initGui();
  }

  private void initGui() {
    getContent().setLayout(new FillLayout(0));

    WidgetContainer container = new WidgetContainer();
    BorderLayout containerLayout = new BorderLayout();
    containerLayout.setMargin(4);
    containerLayout.setSpacing(4);
    container.setLayout(containerLayout);
    getContent().add(container);

    label = new Label();
    container.add(label, new BorderLayoutData(Style.NORTH, 0.3f));

    textBox = new TextBox();
    container.add(textBox, new BorderLayoutData(Style.CENTER, 0.7f));

    okButton = new Button("Ok");
    okButton.addSelectionListener(new SelectionListener() {
      public void widgetSelected(BaseEvent be) {
        text = textBox.getText();
        close();
      }
    });
    addButton(okButton);

    cancelButton = new Button("Cancel");
    cancelButton.addSelectionListener(new SelectionListener() {
      public void widgetSelected(BaseEvent be) {
        close();
      }
    });
    addButton(cancelButton);

    setSize(250, 110);
  }

  public void addOkListener(SelectionListener selectionListener) {
    okButton.addSelectionListener(selectionListener);
  }

  public void addCancelListener(SelectionListener selectionListener) {
    cancelButton.addSelectionListener(selectionListener);
  }

  public void setTitle(String title) {
    setText(title);
  }

  public String getMessageText() {
    return label.getText();
  }

  public void setMessageText(String text) {
    label.setText(text);
  }

  public String getInputText() {
    text = textBox.getText();
    return text;
  }

  public void setInputText(String text) {
    this.text = text;
    textBox.setText(text);
  }
}
